/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.games.epkb.controller;

import it.polimi.games.epkb.controller.exceptions.NonexistentEntityException;
import it.polimi.games.epkb.controller.exceptions.PreexistingEntityException;
import it.polimi.games.epkb.entityClasses.GpiKpiInstance;
import it.polimi.games.epkb.entityClasses.GpiKpiInstancePK;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import it.polimi.games.epkb.entityClasses.SensorType;
import it.polimi.games.epkb.entityClasses.GpiKpi;
import it.polimi.games.epkb.entityClasses.Component;
import javax.transaction.UserTransaction;

/**
 *
 * @author plebani
 */
public class GpiKpiInstanceJpaController implements Serializable {

    public GpiKpiInstanceJpaController(EntityManagerFactory emf) {
        this.utx = null;
        this.emf = emf;
    }
    public GpiKpiInstanceJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(GpiKpiInstance gpiKpiInstance) throws PreexistingEntityException, Exception {
        if (gpiKpiInstance.getGpiKpiInstancePK() == null) {
            gpiKpiInstance.setGpiKpiInstancePK(new GpiKpiInstancePK());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            SensorType idSensorType = gpiKpiInstance.getIdSensorType();
            if (idSensorType != null) {
                idSensorType = em.getReference(idSensorType.getClass(), idSensorType.getId());
                gpiKpiInstance.setIdSensorType(idSensorType);
            }
            GpiKpi idGPIKPI = gpiKpiInstance.getIdGPIKPI();
            if (idGPIKPI != null) {
                idGPIKPI = em.getReference(idGPIKPI.getClass(), idGPIKPI.getId());
                gpiKpiInstance.setIdGPIKPI(idGPIKPI);
            }
            GpiKpi idReferredGPIKPI = gpiKpiInstance.getIdReferredGPIKPI();
            if (idReferredGPIKPI != null) {
                idReferredGPIKPI = em.getReference(idReferredGPIKPI.getClass(), idReferredGPIKPI.getId());
                gpiKpiInstance.setIdReferredGPIKPI(idReferredGPIKPI);
            }
            Component idComponent = gpiKpiInstance.getIdComponent();
            if (idComponent != null) {
                idComponent = em.getReference(idComponent.getClass(), idComponent.getId());
                gpiKpiInstance.setIdComponent(idComponent);
            }
            em.persist(gpiKpiInstance);
            if (idSensorType != null) {
                idSensorType.getGpiKpiInstanceCollection().add(gpiKpiInstance);
                idSensorType = em.merge(idSensorType);
            }
            if (idGPIKPI != null) {
                idGPIKPI.getGpiKpiInstanceCollection().add(gpiKpiInstance);
                idGPIKPI = em.merge(idGPIKPI);
            }
            if (idReferredGPIKPI != null) {
                idReferredGPIKPI.getGpiKpiInstanceCollection().add(gpiKpiInstance);
                idReferredGPIKPI = em.merge(idReferredGPIKPI);
            }
            if (idComponent != null) {
                idComponent.getGpiKpiInstanceCollection().add(gpiKpiInstance);
                idComponent = em.merge(idComponent);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findGpiKpiInstance(gpiKpiInstance.getGpiKpiInstancePK()) != null) {
                throw new PreexistingEntityException("GpiKpiInstance " + gpiKpiInstance + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(GpiKpiInstance gpiKpiInstance) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            GpiKpiInstance persistentGpiKpiInstance = em.find(GpiKpiInstance.class, gpiKpiInstance.getGpiKpiInstancePK());
            SensorType idSensorTypeOld = persistentGpiKpiInstance.getIdSensorType();
            SensorType idSensorTypeNew = gpiKpiInstance.getIdSensorType();
            GpiKpi idGPIKPIOld = persistentGpiKpiInstance.getIdGPIKPI();
            GpiKpi idGPIKPINew = gpiKpiInstance.getIdGPIKPI();
            GpiKpi idReferredGPIKPIOld = persistentGpiKpiInstance.getIdReferredGPIKPI();
            GpiKpi idReferredGPIKPINew = gpiKpiInstance.getIdReferredGPIKPI();
            Component idComponentOld = persistentGpiKpiInstance.getIdComponent();
            Component idComponentNew = gpiKpiInstance.getIdComponent();
            if (idSensorTypeNew != null) {
                idSensorTypeNew = em.getReference(idSensorTypeNew.getClass(), idSensorTypeNew.getId());
                gpiKpiInstance.setIdSensorType(idSensorTypeNew);
            }
            if (idGPIKPINew != null) {
                idGPIKPINew = em.getReference(idGPIKPINew.getClass(), idGPIKPINew.getId());
                gpiKpiInstance.setIdGPIKPI(idGPIKPINew);
            }
            if (idReferredGPIKPINew != null) {
                idReferredGPIKPINew = em.getReference(idReferredGPIKPINew.getClass(), idReferredGPIKPINew.getId());
                gpiKpiInstance.setIdReferredGPIKPI(idReferredGPIKPINew);
            }
            if (idComponentNew != null) {
                idComponentNew = em.getReference(idComponentNew.getClass(), idComponentNew.getId());
                gpiKpiInstance.setIdComponent(idComponentNew);
            }
            gpiKpiInstance = em.merge(gpiKpiInstance);
            if (idSensorTypeOld != null && !idSensorTypeOld.equals(idSensorTypeNew)) {
                idSensorTypeOld.getGpiKpiInstanceCollection().remove(gpiKpiInstance);
                idSensorTypeOld = em.merge(idSensorTypeOld);
            }
            if (idSensorTypeNew != null && !idSensorTypeNew.equals(idSensorTypeOld)) {
                idSensorTypeNew.getGpiKpiInstanceCollection().add(gpiKpiInstance);
                idSensorTypeNew = em.merge(idSensorTypeNew);
            }
            if (idGPIKPIOld != null && !idGPIKPIOld.equals(idGPIKPINew)) {
                idGPIKPIOld.getGpiKpiInstanceCollection().remove(gpiKpiInstance);
                idGPIKPIOld = em.merge(idGPIKPIOld);
            }
            if (idGPIKPINew != null && !idGPIKPINew.equals(idGPIKPIOld)) {
                idGPIKPINew.getGpiKpiInstanceCollection().add(gpiKpiInstance);
                idGPIKPINew = em.merge(idGPIKPINew);
            }
            if (idReferredGPIKPIOld != null && !idReferredGPIKPIOld.equals(idReferredGPIKPINew)) {
                idReferredGPIKPIOld.getGpiKpiInstanceCollection().remove(gpiKpiInstance);
                idReferredGPIKPIOld = em.merge(idReferredGPIKPIOld);
            }
            if (idReferredGPIKPINew != null && !idReferredGPIKPINew.equals(idReferredGPIKPIOld)) {
                idReferredGPIKPINew.getGpiKpiInstanceCollection().add(gpiKpiInstance);
                idReferredGPIKPINew = em.merge(idReferredGPIKPINew);
            }
            if (idComponentOld != null && !idComponentOld.equals(idComponentNew)) {
                idComponentOld.getGpiKpiInstanceCollection().remove(gpiKpiInstance);
                idComponentOld = em.merge(idComponentOld);
            }
            if (idComponentNew != null && !idComponentNew.equals(idComponentOld)) {
                idComponentNew.getGpiKpiInstanceCollection().add(gpiKpiInstance);
                idComponentNew = em.merge(idComponentNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                GpiKpiInstancePK id = gpiKpiInstance.getGpiKpiInstancePK();
                if (findGpiKpiInstance(id) == null) {
                    throw new NonexistentEntityException("The gpiKpiInstance with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(GpiKpiInstancePK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            GpiKpiInstance gpiKpiInstance;
            try {
                gpiKpiInstance = em.getReference(GpiKpiInstance.class, id);
                gpiKpiInstance.getGpiKpiInstancePK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The gpiKpiInstance with id " + id + " no longer exists.", enfe);
            }
            SensorType idSensorType = gpiKpiInstance.getIdSensorType();
            if (idSensorType != null) {
                idSensorType.getGpiKpiInstanceCollection().remove(gpiKpiInstance);
                idSensorType = em.merge(idSensorType);
            }
            GpiKpi idGPIKPI = gpiKpiInstance.getIdGPIKPI();
            if (idGPIKPI != null) {
                idGPIKPI.getGpiKpiInstanceCollection().remove(gpiKpiInstance);
                idGPIKPI = em.merge(idGPIKPI);
            }
            GpiKpi idReferredGPIKPI = gpiKpiInstance.getIdReferredGPIKPI();
            if (idReferredGPIKPI != null) {
                idReferredGPIKPI.getGpiKpiInstanceCollection().remove(gpiKpiInstance);
                idReferredGPIKPI = em.merge(idReferredGPIKPI);
            }
            Component idComponent = gpiKpiInstance.getIdComponent();
            if (idComponent != null) {
                idComponent.getGpiKpiInstanceCollection().remove(gpiKpiInstance);
                idComponent = em.merge(idComponent);
            }
            em.remove(gpiKpiInstance);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<GpiKpiInstance> findGpiKpiInstanceEntities() {
        return findGpiKpiInstanceEntities(true, -1, -1);
    }

    public List<GpiKpiInstance> findGpiKpiInstanceEntities(int maxResults, int firstResult) {
        return findGpiKpiInstanceEntities(false, maxResults, firstResult);
    }

    private List<GpiKpiInstance> findGpiKpiInstanceEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(GpiKpiInstance.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public GpiKpiInstance findGpiKpiInstance(GpiKpiInstancePK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(GpiKpiInstance.class, id);
        } finally {
            em.close();
        }
    }

    public int getGpiKpiInstanceCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<GpiKpiInstance> rt = cq.from(GpiKpiInstance.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
